#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:student.py
# author:wmf
# datetime:2021/5/27 17:00
# software: PyCharm
'''
this is function description
'''
# import module your need
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base

from homework10.homework10_3 import session

Base = declarative_base()


class User(Base):
    __tablename__ = 'student'

    id = Column(Integer, primary_key=True, autoincrement=True)
    age = Column(Integer)
    name = Column(String(255))
    classID = Column(Integer)
    sex = Column(String(255))
    isdeleted = Column(Integer)

    @classmethod
    def dbinsert(cls, **kwargs):
        try:
            student = User(name=kwargs.get('name'),
                        age=kwargs.get('age'),
                        classID=kwargs.get('classID'),
                        sex=kwargs.get('sex'),
                        isdeleted=kwargs.get('isdeleted'))
            session.add(student)
            session.commit()
            session.close()
        except Exception as e:
            session.rollback()
            session.close()
            print(e)
            return {'message': '数据库异常，更新失败', 'error': str(e)}
        return {'message': 'OK'}

    @classmethod
    def get_people(cls, **kwargs):
        try:
            students = session.query(cls).all()
            if not students:
                return {'message': '没有学生信息', 'error': '没有学生信息'}
            else:
                return students
            session.close()
        except Exception as e:
            return {'message': '数据库异常，获取学生信息失败', 'error': str(e)}

    @classmethod
    def get_people_byID(cls, **kwargs):
        id = kwargs.get("id")
        try:
            students = session.query(cls).filter_by(id=id)
            if not students:
                return {'message': '没有学生信息', 'error': '没有学生信息'}
            else:
                return students
            session.close()
        except Exception as e:
            return {'message': '数据库异常，获取学生信息失败', 'error': str(e)}